using DG.Tweening;
using UnityEngine;

public class Trampoline : MonoBehaviour
{
	public float force = 10f;

	public GameObject trampoline;

	private float trampolineInitialY;

	private Sequence animationSeq;

	private bool firstTime = true;

	private void Start()
	{
		Object.Destroy(base.gameObject.GetComponent<MeshFilter>());
		Object.Destroy(base.gameObject.GetComponent<MeshRenderer>());
		trampolineInitialY = trampoline.transform.position.y;
	}

	public void SetForce(float newForce)
	{
		force = newForce;
	}

	private void OnTriggerEnter(Collider other)
	{
		if (other.gameObject == BallBehaviour.GetInstance().gameObject)
		{
			Rigidbody rigidbody = BallBehaviour.GetInstance().gameObject.GetComponent<Rigidbody>();
			Vector3 up = Vector3.up;
			up = base.transform.TransformDirection(up).normalized;
			Vector3 direction = base.transform.InverseTransformDirection(rigidbody.velocity);
			direction.y = 0f;
			direction = base.transform.TransformDirection(direction);
			rigidbody.velocity = direction + up * force;
			PlaySound();
			DoAnimation();
		}
	}

	private void DoAnimation()
	{
		if (firstTime)
		{
			trampolineInitialY = trampoline.transform.localPosition.y;
			firstTime = false;
		}
		if (animationSeq != null)
		{
			animationSeq.Kill();
			animationSeq = null;
		}
		animationSeq = DOTween.Sequence();
		animationSeq.SetAutoKill(true);
		animationSeq.Append(trampoline.transform.DOLocalMoveY(trampolineInitialY + 0.4f, 0.08f).SetEase(Ease.Linear).SetRelative(false));
		animationSeq.Append(trampoline.transform.DOLocalMoveY(trampolineInitialY, 0.08f).SetEase(Ease.Linear).SetRelative(false));
		animationSeq.Play();
	}

	private void PlaySound()
	{
		AudioSource audioSource = GetComponent<AudioSource>();
		if (audioSource == null)
		{
			audioSource = base.gameObject.AddComponent<AudioSource>();
		}
		Audio.PlaySoundTrampoline(audioSource);
	}
}
